package com.kaifamiao.base.Demo20210927;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author lucien-ma
 * @date 2021/9/27
 * @apiNote
 */
public class ReflectDemo {
    public static void main(String[] args) {
        // 获取 Class 对象
        Class<A> clazz = A.class;
        // 获取 A 类的所有 public 构造方法
        Constructor[] cons1 = clazz.getConstructors();
        for (Constructor constructor : cons1) {
            System.out.println(constructor);
        }
        System.out.println("---------------------------------");
        // 获取所有声明的构造方法
        Constructor[] cons2 = clazz.getDeclaredConstructors();
        for (Constructor constructor : cons2) {
            System.out.println(constructor);
        }
        System.out.println("---------------------------------");
        // 获取方法
        Method[] methods1 = clazz.getMethods();
        for (Method method : methods1) {
            System.out.println(method);
        }
        System.out.println("---------------------------------");
        Method[] methods2 = clazz.getDeclaredMethods();
        for (Method method : methods2) {
            System.out.println(method);
        }
        System.out.println("---------------------------------");
        // 获取所有属性
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }
}
